<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <link rel="stylesheet" href="../css/register.css">
</head>
<body>
    <div class="header">
        <a href="#" class="logo">在线购物商城</a>
    </div>

    <div class="container">
        <div class="register-image">
            <img src="../images/register.png" alt="">
        </div>

        <div class="register-form">
            <div class="register-title">账户注册</div>
            <form id="registerForm" onsubmit="return validateForm(event)">
                <div class="form-group">
                    <div class="form-item">
                        <label for="name">姓名</label>
                        <input type="text" id="name" name="name" required>
                    </div>
                    <div class="error-message" id="nameError"></div>
                </div>

                <div class="form-group">
                    <div class="form-item">
                        <label for="idCard">身份证号</label>
                        <input type="text" id="idCard" name="idCard" required>
                    </div>
                    <div class="error-message" id="idCardError"></div>
                </div>

                <div class="form-group">
                    <div class="form-item">
                        <label for="username">用户名</label>
                        <input type="text" id="username" name="username" required>
                    </div>
                    <div class="error-message" id="usernameError"></div>
                </div>

                <div class="form-group">
                    <div class="form-item">
                        <label for="password">登录密码</label>
                        <input type="password" id="password" name="password" required>
                    </div>
                    <div class="error-message" id="passwordError"></div>
                </div>

                <div class="form-group">
                    <div class="form-item">
                        <label for="phone">手机号码</label>
                        <input type="tel" id="phone" name="phone" required>
                    </div>
                    <div class="error-message" id="phoneError"></div>
                </div>

                <button type="submit" class="register-btn">注册</button>
            </form>

            <div class="login-link">
                <a href="login.html">已有账号？立即登录</a>
            </div>
        </div>
    </div>

    <script>
        function validateForm(event) {
            event.preventDefault();
            let isValid = true;

            // 清除所有错误提示
            document.querySelectorAll('.error-message').forEach(elem => {
                elem.style.display = 'none';
            });

            // 验证姓名
            const name = document.getElementById('name').value;
            if (!/^[\u4e00-\u9fa5]{2,6}$/.test(name)) {
                document.getElementById('nameError').textContent = '请输入2-6个汉字的姓名';
                document.getElementById('nameError').style.display = 'block';
                isValid = false;
            }

            // 验证身份证号
            const idCard = document.getElementById('idCard').value;
            if (!validateIdCard(idCard)) {
                document.getElementById('idCardError').textContent = '请输入有效的身份证号码';
                document.getElementById('idCardError').style.display = 'block';
                isValid = false;
            }

            // 验证用户名
            const username = document.getElementById('username').value;
            if (!/^[a-zA-Z0-9_]{4,16}$/.test(username)) {
                document.getElementById('usernameError').textContent = '登录名必须是4-16位字母、数字或下划线';
                document.getElementById('usernameError').style.display = 'block';
                isValid = false;
            }

            // 验证密码
            const password = document.getElementById('password').value;
            if (!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,16}$/.test(password)) {
                document.getElementById('passwordError').textContent = '密码必须包含大小写字母和数字，长度8-16位';
                document.getElementById('passwordError').style.display = 'block';
                isValid = false;
            }

            // 验证手机号
            const phone = document.getElementById('phone').value;
            if (!/^1[3-9]\d{9}$/.test(phone)) {
                document.getElementById('phoneError').textContent = '请输入有效的手机号码';
                document.getElementById('phoneError').style.display = 'block';
                isValid = false;
            }
            if (isValid) {
                // 这里可以添加表单提交的代码
                alert('注册成功！');
                window.location.href = 'login.html';
            }
            return false;
        }

        function validateIdCard(idCard) {
            // 身份证号码验证
            const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
            if (!reg.test(idCard)) {
                return false;
            }
            if (idCard.length === 18) {
                const idCardWi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                const idCardY = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
                let idCardWiSum = 0;

                for (let i = 0; i < 17; i++) {
                    idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
                }

                const idCardMod = idCardWiSum % 11;
                const idCardLast = idCard.substring(17);

                if (idCardY[idCardMod].toLowerCase() !== idCardLast.toLowerCase()) {
                    return false;
                }
            }
            return true;
        }
    </script>
</body>
</html> 